package com.emex.app.stuff.gui;

import com.emex.app.stuff.security.SecurityManager;
import com.emex.app.shared.data.Position;
import com.emex.app.stuff.gui.model.PositionTableModel;
import com.emex.app.stuff.provider.ShopDataProvider;
import com.emex.app.stuff.utils.StringUtilities;
import com.emex.app.stuff.utils.UIUtils;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.openide.windows.WindowManager;

/**
 *
 * @author Sergey Karavaev
 */
public class PositionDialog extends javax.swing.JDialog {

    private PositionTableModel positionTableModel;
    private PositionTableModel analogsTableModel;
    private Position position;
    boolean isCreate = false;

    /**
     * Creates new form PositionDialog
     */
    public PositionDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        analogsPanel.setVisible(false);
        revalidate();
        pack();
        if (!SecurityManager.getDefault().getCurrentPerson().getIsAdmin()) {
            deleteAnalog.setVisible(false);
            minAmountSpinner.setEnabled(false);
        }
    }

    public PositionTableModel getPositionTableModel() {
        return positionTableModel;
    }

    public void setPositionTableModel(PositionTableModel positionTableModel) {
        this.positionTableModel = positionTableModel;
    }

    public Position getPosition() {
        return position;
    }

    public void setPosition(Position position) {
        this.position = position;
        if (null != position) {
            codeTf.setText(position.getCode());
            nameTf.setText(position.getName());
            issuerTf.setText(position.getIssuer());
            priceTf.setText(position.getPrice().toString());
            puPriceTf.setText(position.getPurPrice().toString());
            if (!SecurityManager.getDefault().getCurrentPerson().getIsAdmin()) {
                amountSpinner.setModel(new SpinnerNumberModel(position.getAmount().intValue(), position.getAmount().intValue(), 200, 1));
            } else {
                amountSpinner.setValue(position.getAmount());
            }
            minAmountSpinner.setValue(position.getMinAmount());
            placeTf.setText(position.getPlace());

            List<Position> positions = ShopDataProvider.getInstance().getCrossPositionsByPositionId(position.getId());
            analogsTableModel = new PositionTableModel(positions);
            posAnalogsTable.setModel(analogsTableModel);
            posAnalogsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    int row = posAnalogsTable.getSelectedRow();
                    if ((row >= 0) && (row < posAnalogsTable.getRowCount())) {
                        deleteAnalog.setEnabled(true);
                    } else {
                        deleteAnalog.setEnabled(false);
                    }
                }
            });

            revalidate();
            pack();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        codeLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        issuerLabel = new javax.swing.JLabel();
        priceLabel = new javax.swing.JLabel();
        purPriceLabel = new javax.swing.JLabel();
        amountLabel = new javax.swing.JLabel();
        minAmountLabel = new javax.swing.JLabel();
        placeLabel = new javax.swing.JLabel();
        codeTf = new javax.swing.JTextField();
        nameTf = new javax.swing.JTextField();
        issuerTf = new javax.swing.JTextField();
        priceTf = new javax.swing.JTextField();
        puPriceTf = new javax.swing.JTextField();
        amountSpinner = new javax.swing.JSpinner();
        minAmountSpinner = new javax.swing.JSpinner();
        placeTf = new javax.swing.JTextField();
        cancelButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        analogsPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        posAnalogsTable = new org.jdesktop.swingx.JXTable();
        addAnalogButton = new javax.swing.JButton();
        deleteAnalog = new javax.swing.JButton();
        showHideAnalogs = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(codeLabel, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.codeLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(nameLabel, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.nameLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(issuerLabel, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.issuerLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(priceLabel, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.priceLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(purPriceLabel, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.purPriceLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(amountLabel, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.amountLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(minAmountLabel, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.minAmountLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(placeLabel, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.placeLabel.text")); // NOI18N

        codeTf.setText(org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.codeTf.text")); // NOI18N

        nameTf.setText(org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.nameTf.text")); // NOI18N

        issuerTf.setText(org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.issuerTf.text")); // NOI18N

        priceTf.setText(org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.priceTf.text")); // NOI18N

        puPriceTf.setText(org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.puPriceTf.text")); // NOI18N

        placeTf.setText(org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.placeTf.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.cancelButton.text")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(saveButton, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.saveButton.text")); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        analogsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.analogsPanel.border.title"))); // NOI18N

        jScrollPane1.setViewportView(posAnalogsTable);

        addAnalogButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(addAnalogButton, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.addAnalogButton.text")); // NOI18N
        addAnalogButton.setToolTipText(org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.addAnalogButton.toolTipText")); // NOI18N
        addAnalogButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAnalogButtonActionPerformed(evt);
            }
        });

        deleteAnalog.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(deleteAnalog, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.deleteAnalog.text")); // NOI18N
        deleteAnalog.setToolTipText(org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.deleteAnalog.toolTipText")); // NOI18N
        deleteAnalog.setEnabled(false);
        deleteAnalog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteAnalogActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout analogsPanelLayout = new javax.swing.GroupLayout(analogsPanel);
        analogsPanel.setLayout(analogsPanelLayout);
        analogsPanelLayout.setHorizontalGroup(
            analogsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(analogsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(analogsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(analogsPanelLayout.createSequentialGroup()
                        .addComponent(addAnalogButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteAnalog, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        analogsPanelLayout.setVerticalGroup(
            analogsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, analogsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(analogsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addAnalogButton)
                    .addComponent(deleteAnalog))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        org.openide.awt.Mnemonics.setLocalizedText(showHideAnalogs, org.openide.util.NbBundle.getMessage(PositionDialog.class, "PositionDialog.showHideAnalogs.text")); // NOI18N
        showHideAnalogs.setEnabled(false);
        showHideAnalogs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showHideAnalogsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(analogsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                            .addComponent(codeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(placeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(issuerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(placeTf, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                            .addComponent(nameTf, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(codeTf, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(issuerTf, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(priceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(purPriceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(amountLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(minAmountLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(priceTf, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                .addComponent(puPriceTf)
                                .addComponent(amountSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(minAmountSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(showHideAnalogs, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(codeLabel)
                    .addComponent(codeTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(priceLabel)
                    .addComponent(priceTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(purPriceLabel)
                    .addComponent(puPriceTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(issuerLabel)
                    .addComponent(issuerTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(amountLabel)
                    .addComponent(amountSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(placeLabel)
                    .addComponent(placeTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(minAmountLabel)
                    .addComponent(minAmountSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addComponent(showHideAnalogs)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(analogsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(cancelButton))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        if (!validateProps()) {
            return;
        }
        String code = StringUtilities.formatPositionCode(StringUtilities.formatStringFromSpecialCharacters(codeTf.getText())).toUpperCase();
        position.setCode(code);
        position.setName(nameTf.getText());
        position.setIssuer(issuerTf.getText().toUpperCase());
        position.setPrice(Double.parseDouble(priceTf.getText().replaceAll(",", ".")));
        position.setPurPrice(Double.parseDouble(puPriceTf.getText().replaceAll(",", ".")));
        position.setAmount(Integer.valueOf(amountSpinner.getValue().toString()));
        position.setMinAmount(Integer.valueOf(minAmountSpinner.getValue().toString()));
        position.setPlace(placeTf.getText());

        if (isCreate) {
            List<Position> existing = ShopDataProvider.getInstance().getPositionByCode(code);
            if (null != existing && !existing.isEmpty()) {
                StringBuilder analogs = new StringBuilder();
                analogs.append("\n");
                for (Position p: existing) {
                    analogs.append(p.getName()).append(", ").append(p.getIssuer()).append("\n");
                }
                if (UIUtils.showConfirmation("Позиция(и) с таким кодом уже существует(ют):" + analogs.toString() + "Все равно сохранить?") == JOptionPane.YES_OPTION) {
                    position = ShopDataProvider.getInstance().insertPosition(position);
                    if (null != positionTableModel) {
                        positionTableModel.dataAdded(position);
                    }
                }
            } else {
                position = ShopDataProvider.getInstance().insertPosition(position);
                if (null != positionTableModel) {
                    positionTableModel.dataAdded(position);
                }
            }
        } else {
            ShopDataProvider.getInstance().updatePosition(position);
            if (null != positionTableModel) {
                positionTableModel.dataUpdated();
            }
        }

        setVisible(false);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void showHideAnalogsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showHideAnalogsActionPerformed
        if (analogsPanel.isVisible()) {
            analogsPanel.setVisible(false);
            showHideAnalogs.setText("Показать аналоги");
        } else {
            analogsPanel.setVisible(true);
            showHideAnalogs.setText("Скрыть аналоги");
        }
        revalidate();
        pack();
    }//GEN-LAST:event_showHideAnalogsActionPerformed

    private void addAnalogButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAnalogButtonActionPerformed
        SearchAnalogDialog dialog = new SearchAnalogDialog(WindowManager.getDefault().getMainWindow(), true);
        dialog.setOriginalPosition(position);
        dialog.setAnalogsTableModel(analogsTableModel);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
    }//GEN-LAST:event_addAnalogButtonActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        if (position == null) {
            isCreate = true;
            position = new Position();
        } else {
            showHideAnalogs.setEnabled(true);
        }
    }//GEN-LAST:event_formWindowOpened

    private void deleteAnalogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteAnalogActionPerformed
        Position analog = getAnalogSelection();
        if (null != analog && null != position) {
            ShopDataProvider.getInstance().deleteCrosByPositions(analog, position);
            analogsTableModel.dataRemoved(analog);
        }
    }//GEN-LAST:event_deleteAnalogActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAnalogButton;
    private javax.swing.JLabel amountLabel;
    private javax.swing.JSpinner amountSpinner;
    private javax.swing.JPanel analogsPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel codeLabel;
    private javax.swing.JTextField codeTf;
    private javax.swing.JButton deleteAnalog;
    private javax.swing.JLabel issuerLabel;
    private javax.swing.JTextField issuerTf;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel minAmountLabel;
    private javax.swing.JSpinner minAmountSpinner;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTf;
    private javax.swing.JLabel placeLabel;
    private javax.swing.JTextField placeTf;
    private org.jdesktop.swingx.JXTable posAnalogsTable;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JTextField priceTf;
    private javax.swing.JTextField puPriceTf;
    private javax.swing.JLabel purPriceLabel;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton showHideAnalogs;
    // End of variables declaration//GEN-END:variables

    private boolean validateProps() {
        StringBuilder msg = new StringBuilder();
        boolean isValid = true;
        msg.append("Следующие поля обязательны для заполнения: ");
        if (codeTf.getText() == null || codeTf.getText().isEmpty()) {
            msg.append(" - Код запчасти").append("\n");
            isValid = false;
        }
        if (nameTf.getText() == null || nameTf.getText().isEmpty()) {
            msg.append(" - Наименование запчасти").append("\n");
            isValid = false;
        }
        if (priceTf.getText() == null || priceTf.getText().isEmpty()) {
            msg.append(" - Цена запчасти").append("\n");
            isValid = false;
        }
        if (puPriceTf.getText() == null || puPriceTf.getText().isEmpty()) {
            msg.append(" - Закупочная цена запчасти").append("\n");
            isValid = false;
        }
        if (amountSpinner.getValue() == null) {
            msg.append(" - Количество").append("\n");
            isValid = false;
        }
        if (minAmountSpinner.getValue() == null) {
            msg.append(" - Мин. остаток").append("\n");
            isValid = false;
        }
        if (!isValid) {
            UIUtils.showErrorMessage(msg.toString());
        }

        return isValid;
    }

    protected Position getAnalogSelection() {
        if (posAnalogsTable.getSelectedRow() > -1) {
            Position pos = (Position) posAnalogsTable
                    .getValueAt(posAnalogsTable.getSelectedRow(), -1);
            return pos;
        }
        return null;
    }
}
